﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace GieldaCSharp
{
    public partial class Portfel : Form
    {
        private List<string> portfel = new List<string>();
        private List<string> spolki = new List<string>();

        public bool inUse = true;
        public bool uruchomPrzyStarcie;

        public Portfel(List<string> _spolki, List<string> _portfel)
        {
            Form1.zmiana = false;
            InitializeComponent();
            portfel = _portfel;
            spolki = _spolki;
            uruchomPrzyStarcie = Form1.uruchomPrzyStarcie;

            
            txtEMA.Text = Form1.EMA.ToString();
            txtInfl.Text = String.Format("{0:f}", Form1.Infl);
            txtPrK.Text = String.Format("{0:f}", Form1.PrK);
            txtPrS.Text = String.Format("{0:f}", Form1.PrS);
            txtEMAprp.Text = String.Format("{0:f}", Form1.EMAprp);
            txtEMAprm.Text = String.Format("{0:f}", Form1.EMAprm );

        }

        private void Portfel_Load(object sender, EventArgs e)
        {
            foreach (string s in spolki)
            {
                checkedListBox1.Items.Add(s);
            }


            if (portfel.Count > 0)
                if (portfel[0] != null)
                    foreach (string s in portfel)
                    {
                        checkedListBox1.SetItemChecked(checkedListBox1.FindString(s), true);
                    }

            checkUruchom.Checked = uruchomPrzyStarcie;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(Application.StartupPath.ToString() + "\\bin")) Directory.CreateDirectory(Application.StartupPath.ToString() + "\\bin");

            FileStream fs = new FileStream(Application.StartupPath.ToString() + "\\bin\\portfel.ini", FileMode.Create);
            StreamWriter sr = new StreamWriter(fs);

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    sr.WriteLine(checkedListBox1.Items[i].ToString());
                }
            }
            sr.Close();

            Form1.uruchomPrzyStarcie = uruchomPrzyStarcie;

            if (!int.TryParse(txtEMA.Text, out Form1.EMA))
            {
                MessageBox.Show("Błędna wartośc EMA!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (!double.TryParse(txtInfl.Text, out Form1.Infl))
            {
                MessageBox.Show("Błędna wartośc inflacji!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }

            if (!double.TryParse(txtPrK.Text, out Form1.PrK))
            {
                MessageBox.Show("Błędna wartośc PrK!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }

            if (!double.TryParse(txtPrS.Text, out Form1.PrS))
            {
                MessageBox.Show("Błędna wartośc PrS!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }

            if (!double.TryParse(txtEMAprp.Text, out Form1.EMAprp))
            {
                MessageBox.Show("Błędna wartośc EMAprp!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }
            if (!double.TryParse(txtEMAprm.Text, out Form1.EMAprm))
            {
                MessageBox.Show("Błędna wartośc EMAprm!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                
            }

            Form1.zmiana = true;
            this.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Text == "Portfel")
            {
                groupPortfel.Visible = true;
                groupUruchom.Visible = false;
                groupOpcje.Visible = false;
            }
            if (treeView1.SelectedNode.Text == "Uruchamianie")
            {
                groupPortfel.Visible = false;
                groupUruchom.Visible = true;
                groupOpcje.Visible = false;
            }
            if (treeView1.SelectedNode.Text == "Parametry")
            {
                groupPortfel.Visible = false;
                groupUruchom.Visible = false;
                groupOpcje.Visible = true;
            }
        }

        private void checkUruchom_CheckedChanged(object sender, EventArgs e)
        {
            uruchomPrzyStarcie = checkUruchom.Checked;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, true);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, false);
            }
        }
    }
}
